<template>
  <div class="inventory-management">
    <div class="page-header">
      <h2>库存管理</h2>
      <el-button type="primary" @click="refreshInventory">刷新库存</el-button>
    </div>

    <el-table :data="inventory" style="width: 100%">
      <el-table-column prop="id" label="商品ID" width="100" />
      <el-table-column prop="name" label="商品名称" />
      <el-table-column prop="stock" label="当前库存" />
      <el-table-column prop="minStock" label="最低库存" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="getStockStatusType(scope.row)">
            {{ getStockStatusText(scope.row) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="updateStock(scope.row)">
            修改库存
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';

const inventory = ref([
  {
    id: 1,
    name: '夹心饼干',
    stock: 100,
    minStock: 50,
  },
  {
    id: 2,
    name: '曲奇饼干',
    stock: 30,
    minStock: 50,
  }
]);

const getStockStatusType = (item) => {
  if (item.stock <= item.minStock) {
    return 'warning';
  }
  return 'success';
};

const getStockStatusText = (item) => {
  if (item.stock <= item.minStock) {
    return '库存不足';
  }
  return '库存充足';
};

const refreshInventory = () => {
  ElMessage.success('库存数据已更新');
};

const updateStock = (item) => {
  ElMessage.success(`修改商品 ${item.name} 的库存`);
};
</script>

<style scoped>
.inventory-management {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

h2 {
  margin: 0;
  color: #2c3e50;
}
</style>